package com.jvyou.app.ds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jvyou.app.common.domain.model.PageResult;
import com.jvyou.app.common.utils.orm.WrapperTool;
import com.jvyou.app.ds.domain.dto.FaqDto;
import com.jvyou.app.ds.domain.entity.Faq;
import com.jvyou.app.ds.domain.entity.FaqCategory;
import com.jvyou.app.ds.mapper.FaqCategoryMapper;
import com.jvyou.app.ds.mapper.FaqMapper;
import com.jvyou.app.ds.service.FaqService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 驾校系统常见问题表(Faq)表服务实现类
 *
 * @author 橘柚
 * @since 2025-04-20 12:16:44
 */
@Service("faqService")
@RequiredArgsConstructor
public class FaqServiceImpl extends ServiceImpl<FaqMapper, Faq> implements FaqService {

    private final FaqCategoryMapper faqCategoryMapper;

    private QueryWrapper<Faq> getWrapper(FaqDto faqDto) {
        QueryWrapper<Faq> queryWrapper = new QueryWrapper<>();
        WrapperTool.notEmptyEq(queryWrapper, "category_id", faqDto.getCategoryId());
        WrapperTool.notEmptyEq(queryWrapper, "is_hot", faqDto.getIsHot());
        WrapperTool.notEmptyEq(queryWrapper, "is_recommend", faqDto.getIsRecommend());
        WrapperTool.notEmptyEq(queryWrapper, "status", faqDto.getStatus());
        WrapperTool.notEmptyLike(queryWrapper, "question", faqDto.getQuestion());
        return queryWrapper;
    }
    @Override
    public PageResult<Faq> pageFaq(FaqDto faqDto) {
        IPage<Faq> page = new Page<>(faqDto.getCurrent(), faqDto.getPageSize());
        page = page(page, getWrapper(faqDto));
        for (Faq faq : page.getRecords()) {
            FaqCategory faqCategory = faqCategoryMapper.selectById(faq.getCategoryId());
            faq.setCategoryName(faqCategory.getName());
        }
        return PageResult.success(page);
    }

    @Override
    public List<Faq> listFaq(FaqDto faqDto) {
        return list(getWrapper(faqDto));
    }
}
